package com.yxr.mobileplayer.page;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.yxr.mobileplayer.R;
import com.yxr.mobileplayer.activity.SystemVideoPlayerActivity;
import com.yxr.mobileplayer.adapter.NativeVideoPageAdapter;
import com.yxr.mobileplayer.base.BasePage;
import com.yxr.mobileplayer.entity.MediaItem;
import com.yxr.mobileplayer.tools.LArrayList;

public class NativeVideoPage extends BasePage  {

    private ListView lv_video_page;
    private TextView tv_no_media;
    private ProgressBar pb_loading;
    private LArrayList<MediaItem > mediaItems;


    public NativeVideoPage(Activity activity, LArrayList<MediaItem > mediaItems) {
        super(activity);
        this.mediaItems = mediaItems;
    }


    @Override
    protected View onCreateSelfView() {
        View view = View.inflate(activity, R.layout.native_vidow_page,null);
        //视频类表
        lv_video_page = view.findViewById(R.id.lv_video_page);
        //文字提醒
        tv_no_media = view.findViewById(R.id.tv_no_media);
        //进度条
        pb_loading = view.findViewById(R.id.pb_loading);
        onCreateViewDate();
        //设置点击事件
        lv_video_page.setOnItemClickListener((parent, view1, position, id) -> {
            MediaItem mediaItem = mediaItems.get(position);
//                Intent intent = new Intent();
//                intent.setDataAndType(Uri.parse(mediaItem.getData()),"video/*");
//                context.startActivity(intent);
            Intent intent = new Intent(activity, SystemVideoPlayerActivity.class);
//            intent.setDataAndType(Uri.parse(mediaItem.getData()),"video/*");
            //将这个视频列表存放到mediaItems中
            Bundle bundle = new Bundle();
            bundle.putSerializable("mediaItems",mediaItems);
            //建bundle存放到intent
            intent.putExtras(bundle);
            //建要播放的视频所在位置也存入到intent
            intent.putExtra("position",position);
            activity.startActivity(intent);
        });
        return view;
    }

    @Override
    protected void onCreateViewDate() {
        //加载本地数据
        new Thread(()->{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                activity.requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
            }
        }).start();
    }
    @SuppressLint("Handlerleak")
    private   Handler handler = new Handler(){
        //主线程执行
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1001) {
                if (null == lv_video_page || null == tv_no_media || null == pb_loading) return;
                if (null == mediaItems || mediaItems.size() <= 0) {
                    tv_no_media.setVisibility(View.VISIBLE);
                    pb_loading.setVisibility(View.GONE);
                    return;
                }
                //隐藏提示
                tv_no_media.setVisibility(View.GONE);
                pb_loading.setVisibility(View.GONE);
                //设置适配器
                lv_video_page.setAdapter(new NativeVideoPageAdapter(activity, mediaItems));
            }
        }
    };

    public Handler getHandler(){
        return this.handler;
    }

}
